package org.dyk.seamanshousefrontdesk.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.dyk.seamanshousefrontdesk.config.AliPayConfig;
import org.dyk.seamanshousefrontdesk.service.orders.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/pay")
public class AliPayController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 支付接口
     *
     * @param orderId   订单id
     * @param price     支付金额
     * @param goodsName 商品名称
     * @param body      商品描述
     * @return
     * @throws AlipayApiException /aliPay/{orderId}/{amount}/{product}/{body}
     */
    @RequestMapping("/aliPay/{orderId}/{price}/{goodsName}/{body}")
    public String aliPay(@PathVariable String orderId,
                         @PathVariable String price,
                         @PathVariable String goodsName,
                         @PathVariable String body) throws AlipayApiException {

//获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl,
                AliPayConfig.app_id,
                AliPayConfig.merchant_private_key,
                "json",
                AliPayConfig.charset,
                AliPayConfig.alipay_public_key,
                AliPayConfig.sign_type);
//        page
        AlipayTradePagePayRequest alipayPageRequest = new AlipayTradePagePayRequest();
        alipayPageRequest.setReturnUrl(AliPayConfig.return_url);
        alipayPageRequest.setNotifyUrl(AliPayConfig.notify_url);


        //拼接参数
        alipayPageRequest.setBizContent("{\"out_trade_no\":\"" + orderId + "\","
                + "\"total_amount\":\"" + price + "\","
                + "\"subject\":\"" + goodsName + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        return alipayClient.pageExecute(alipayPageRequest).getBody();
    }

    /**
     * 支付成功的回调 这个回调接口一般使用在支付完成后需要执行的业务比
     *  如 删除该订单 商品数减去相应的数量等等
     * */
    @RequestMapping(value = "/payOut")
    public Object payOut (HttpServletRequest request, HttpServletResponse response, @RequestParam String out_trade_no) {
        Map<String, String[]> parameterMap = request.getParameterMap();

        System.out.println("订单信息{}"+ JSON.toJSON(parameterMap));
        System.out.println(out_trade_no);
        Map map = request.getParameterMap();
        System.out.println("进入了回调");
        //修改订单状态
        int i = Integer.parseInt(out_trade_no);
        ordersService.payOut(i);
        try {
            response.sendRedirect(request.getContextPath() + "/orders");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "支付完成";
    }
}


